package com.thti.mecool.client.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.threeti.mecool.mobile.interfaces.dto.ProjectStoreWithSellIntoDto;
import com.threeti.mecool.mobile.interfaces.dto.ProjectWithStoresAndSellIntoDto;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.command.ManageCommand;
import com.thti.mecool.client.command.result.sellinto.SellIntoCommand;
import com.thti.mecool.client.command.send.saleinto.SellIntoSend;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.ProgressDialogWidget;
import com.thti.mecool.client.widget.Toolbar;

public class SellStoreDetail extends BaseActivity{
	private ListView StoreList;
	private List<ProjectStoreWithSellIntoDto> pDtoList;
	Handler handler;
	ProgressDialogWidget progressDialogWidget;
	int curItem;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		noTitle();
		setContentView(R.layout.mecool_sale_manage_two);
		initToolbar();
		handler=new Handler(){

			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case 0xAA:
					progressDialogWidget.dismiss();
					List<ProjectWithStoresAndSellIntoDto> projwithstoresandsellinto=(List<ProjectWithStoresAndSellIntoDto>)msg.obj;
					pDtoList=projwithstoresandsellinto.get(curItem).getProjStores();
					StoreList.setAdapter(StoreAdapter);
					break;
				case 0xAB:
					progressDialogWidget.dismiss();
					Util.sendToast(SellStoreDetail.this, "更新数据异常");
					break;
				default:
					break;
				}
			}
			
		};
		initData();
		StoreList.setOnItemClickListener(onItemClickListener);
	}
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText("选择终端");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){
	
		@Override
		public void onClick(View v) {
			finish();
	}};
	
	@SuppressWarnings("unchecked")
	private void initData() {
		StoreList=(ListView)findViewById(R.id.mecool_sale_manage_two_listview);
		pDtoList=(List<ProjectStoreWithSellIntoDto>) getIntent().getSerializableExtra("ProjectStoreWithSellIntoDto");
		curItem=getIntent().getIntExtra("item", 0);
		StoreList.setAdapter(StoreAdapter);
	}
	
	private OnItemClickListener onItemClickListener=new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			Intent i=new Intent(SellStoreDetail.this,SellDetailActivity.class);
			i.putExtra("SSD", pDtoList.get(arg2));
			startActivity(i);
		}
	};
	
	private BaseAdapter StoreAdapter = new BaseAdapter() {
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			TextView name,time;
			if(arg1==null){
				arg1=getLayoutInflater().inflate(R.layout.mecool_sell_and_receipt_store_item, null);
				name=(TextView)arg1.findViewById(R.id.mecool_sell_store_item_name);
				time=(TextView)arg1.findViewById(R.id.mecool_sell_store_item_time);
				name.setText(pDtoList.get(arg0).getName());
				time.setText(pDtoList.get(arg0).getSellInto().getLastModified());
			}
			return arg1;
		}
		
		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
		
		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return pDtoList.get(arg0);
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return pDtoList.size();
		}
	};
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SellIntoCommand sellIntoCommand=new SellIntoCommand(handler);
		ManageCommand.addCommand(sellIntoCommand);
		SellIntoSend.getSellInto(getCurrentUser().getLoginName());
		progressDialogWidget=new ProgressDialogWidget(SellStoreDetail.this);
		progressDialogWidget.setDismissTime(16);
	}
	
	
	
}
